/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.IDepotBankInfoDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.DepotBankInfo;

/**
 * @author zsy
 *
 */
public class DepotBankInfoDAO extends BaseHibernateDAO implements IDepotBankInfoDAO {

	@Override
	public void save(DepotBankInfo info) {
		Query query = getSession().getNamedQuery("DepotBankInfoDAO.save");
//		query.setString("companyCode", info.getId().getCompanyCode());
		query.setString("bankCode", info.getBankCode());
		query.setString("bankName", info.getBankName());
		query.setString("occurDate", info.getOccurDate());
		query.setLong("recordNo", info.getRecordNo());
		query.executeUpdate();
	}

	@Override
	public void update(String bankCode, DepotBankInfo info) {
		Query query = getSession().getNamedQuery("DepotBankInfoDAO.update");
//		query.setString("companyCode", info.getId().getCompanyCode());
		query.setString("bankCode", info.getBankCode());
		query.setString("bankName", info.getBankName());
		query.setString("occurDate", info.getOccurDate());
		
//		query.setString("oldCompanyCode", id.getCompanyCode());
		query.setString("oldBankCode", bankCode);
		query.executeUpdate();
	}
	
	@Override
	public void delete(String bankCode) {
		Query query = getSession().getNamedQuery("DepotBankInfoDAO.delete");
//		query.setString("companyCode", id.getCompanyCode());
		query.setString("bankCode", bankCode);
		query.executeUpdate();
		
	}
	
	@Override
	public boolean checkId(String bankCode) {
		Query query = getSession().getNamedQuery("DepotBankInfoDAO.checkId");
//		query.setString("companyCode", id.getCompanyCode());
//		query.setString("companyCode", id.getCompanyCode());
		query.setString("bankCode", bankCode);
		Long count = (Long)query.uniqueResult();
		if (count != null && count.longValue() > 0) {
			return false;
		}
		return true;
	}
	
	@Override
	public long findNextRecordNo() {
		Query query = getSession().getNamedQuery("DepotBankInfoDAO.findNextRecordNo");
		Long count = (Long)query.uniqueResult();
		if (count == null) {
			return 1;
		}
		return count.longValue() + 1;
	}

	@Override
	public ListPage<DepotBankInfo> findListPage(QueryParameters qp) {
		try {
			Query query = getSession().getNamedQuery("DepotBankInfoDAO.findListPage");
			Query countQuery = getSession().getNamedQuery("DepotBankInfoDAO.findListPage_count");
			ListPage<Object> page = new CommQuery<Object>().queryListPage(qp, query, countQuery);
			ListPage<DepotBankInfo> depotPage = new ListPage<DepotBankInfo>();
			depotPage.setTotalCount(page.getTotalCount());
			depotPage.setCurrentPageNo(page.getCurrentPageNo());
			depotPage.setCurrentPageSize(page.getCurrentPageSize());
			if (page != null && page.getDataList() != null) {
				List<DepotBankInfo> datas = new ArrayList<DepotBankInfo>();
				for (Object o : page.getDataList()) {
					Object[] oArr = (Object[])o;
					DepotBankInfo bank = new DepotBankInfo();
					bank.setBankCode(String.valueOf(oArr[0]));
					bank.setBankName(String.valueOf(oArr[1]));
					bank.setOccurDate(String.valueOf(oArr[2]));
					datas.add(bank);
				}
				depotPage.setDataList(datas);
			}
			return depotPage;
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<DepotBankInfo>();
		}
	}
}
